jQuery.easing.def = "easeOutExpo";
var max_z = 100;

var list;

$(document).ready(function() {
	$(".todo-list").draggable();
	$(".task").draggable();
	$(".tasks").sortable();
	$(".tasks").disableSelection();

	list = new TodolistList(youdo_user_id, ".todolist-list");
	list.populate();

	$("#workspace").css({ height: $(document).height() - $("#toolbar").height() - 90});
	$("#workspace, #toolbar").css({width: $(document).width() - 20});

	$(".todolist").click(function() {
	});
});

var TaskDisplay = function(id) {
	$("#modal-mask").fadeIn(50);
	self.dom_id = id+"task_window";
	self.id = id;
	
	self.HTML = "";

	self.draw = function() {
		$("#modal-window").empty().append(self.HTML).hide();
		$("#modal-window").modal({
			opacity: 80,
			overlayCss: {backgroundColor: "#000"},
			overlayClose: true,
		});
	}

	self.populate = function() {
		$.getJSON("php/get_task.php", {task_id: self.id}, function(json) {
			HTML = ""+ 
				"<input type='button' class='task-delete' value='Delete'/>"+
				"<input type='button' class='task-change' value='Change Text'/>"+
				"<hr>"+
				"<div id='"+self.dom_id+"'>"+
				"Created By: <span class='task-owner'>"+json.displayname+"</span></br></br>"+
				"Text: <span class='task-text'>"+json.text+"</span></br></br>"+
				"Comments: <ul class='comments'>";

			for(var i in json.comments) {
				HTML += "<div class='comment'>"+
						"<li><span class='comment-username'>"+json.comments[i].displayname+"</span>"+
						"<span class='comment-text'>"+json.comments[i].text+"</span></li></div>";
			}
			HTML += "</ul>"+
				"<input id='newcomment' type='button' value='add a comment'/>"
				"</div>";
			self.draw();
		});
	}
	self.populate();

	$(".task-delete").live('click', function() {
		$.get("php/get_listid_from_taskid.php", {task_id: self.id}, function(list_id) {
			$.get("php/delete_task.php", {task_id: self.id}, function() {
				$("#modal-window").html("This task has been deleted!");
				var position = $("#todolist"+list_id).position();
				$("#todolist"+list_id).remove();
				var todolist = new TodoList(list_id, position.left, position.top);
			});
		});
	});

	$(".task-change").live('click', function() {
		var prompt_text = prompt("What would you like the task to be?");
		if(prompt_text == null) return true;
		if(prompt_text.length > 0) {
			$.get("php/task_change.php", {task_id: self.id, text: prompt_text}, function(list_id) {
				$(".task-text").html(prompt_text);
				var position = $("#todolist"+list_id).position();
				$("#todolist"+list_id).remove();
				var todolist = new TodoList(list_id, position.left, position.top);
			});
		}
	});

	$("#newcomment").live('click', function(event) {
		event.preventDefault();
		var comment_text = prompt("Please enter a comment:");

		if(comment_text == null) return true;
		else if(comment_text.length > 0) {
			$.get("php/add_task_comment.php", {user_id: youdo_user_id, task_id: self.id, text: comment_text}, function() {
					var HTML = "<div class='comment'><li><span class='comment-username'>Me</span>"+
							"<span class='comment-text'>"+comment_text+"</span></li></div>";
					$(".comments").prepend(HTML);
			});
		}
		else return true;
	});
}

var TodolistList = function(user_id, div) {
	var self = this;
	self.div = div;
	self.populate = function() { 
		$.getJSON('php/get_todolists.php', {user_id: user_id}, function(json) {
			$(self.div).empty();
			for(var list in json) {
				var liHTML = "<li class='todolist-entry'><a href='#' id='"+json[list].id+"_list'>"+json[list].name+"</a></li>";
				$(self.div).append(liHTML);
			}
			
		});
	}

	$("#workspace .lists-show-button").live('click', function() {
		$("#workspace .lists-dropdown").slideToggle(1000);
		self.populate();
	});

	$("#workspace .lists-add-button").live('click', function() {
		var todo_title = prompt("What would you like the name of your new todolist to be?");
		if(todo_title.length > 0) {
			$.get("php/make_todolist.php", {name: todo_title, description: "none.", user_id: youdo_user_id}, function(id) {
				var newlist = new TodoList(id, 500, 500);	
				list.populate();

			});
		}
	});

	$(self.div+" .todolist-entry").live('click', function() {
		var id = $(this).find("a").attr("id");
		id = parseInt(id);
		if($("#todolist"+id).length == 0) var list = new TodoList(parseInt(id), 300, 300);
	});
}

var TodoList = function(id, x, y) {
	var self = this;

	self.sqlid = id;
	self.x = x;
	self.y = y;
	self.id = "todolist"+id;
	self.class = "todo"
	self.title = "Test List";
	self.tasks = [];

	self.HTML = "";

	self.load = function() {
		$.getJSON('php/get_todolist.php', {todolist_id: self.sqlid}, function(json) {
			self.HTML = "<div class='todolist' id='"+self.id+"'>"+
				"<div class='close'>&nbsp;</div>"+
				"<div class='todolist-title'><span class='title'>"+json.name+"</span></div>"+
				"<div class='todolist-tasks'>"+
				"<ul class='tasks'>";
			for(var task in json.tasks) {
				self.HTML += "<li class='todolist-task' id='"+json.tasks[task].id+"task'><input type='checkbox' class='task-checkbox' id='"+json.tasks[task].id+"' ";
				if(json.tasks[task].finished == true) self.HTML += "checked";
				self.HTML += "/><a href='#'><p class='clickable'>"+json.tasks[task].step_no+") "+json.tasks[task].text+"</p></a></li>";
			}
			self.HTML += "</ul>"+
				"<a href='#' class='todolist-addtask'>Add Task</a>"+
				"</div>"+
				"<div class='todolist-sharing'>"+
				"<b>Users sharing this list:</b>"+
				"<ul class='todolist-users'>"+
				"</ul>"+
				"<a href='#' class='todolist-adduser'>Add User</a>"+
				"</div>"+
				"<table class='actions'>"+
				"<tr>"+
				"<td class='share'><a href='#'>Sharing</a></td>"+	
				"<td><a href='#' class='delete'>Delete</a></td>"+	
				"</tr>"+
				"</table>"+
				"</div>";

			self.draw();
		});
	}
	self.load();

	$("#"+self.id+" .todolist-adduser").live('click', function() {
		var username = prompt("Please enter the email address of the user you want to add to this list: ");
		if(username == null) return true;
		else if(username.length > 0) {
			$.get("php/add_todolist_user.php", {todolist_id: self.sqlid, email: username}, function() {
				var HTML = "<li>"+username+"</li>";
				$("#"+self.id+" .todolist-users").append(HTML);
			});
		}
	});

	$("#"+self.id+" .todolist-addtask").live('click', function() {
		var priority;
		var tasktext;
		while(priority = prompt("What priority is this task? (input a number)")) {
			if(priority == "null") return true;
			if(parseInt(priority) > 0) {
				tasktext = prompt("What is your new task?");
				break;
			}
		}

		if(tasktext == null) return true;
		if(tasktext.length > 0) {
			$.get("php/make_task.php", {todolist_id: self.sqlid, user_id: youdo_user_id, text: tasktext, priority: priority}, function(id) {
				var html = "<li class='todolist-task ui-state-default' id='"+id+"task'><input type='checkbox' class='task-checkbox' id='"+id+"' /><p class='clickable'>"+priority+") "+tasktext+"</p></li>";
				$("#"+self.id+" .tasks").append(html);
			});
		}
	});

	$
	$("#"+self.id+" .share").live('click', function() {
		$.getJSON('php/get_todolist_users.php', {todolist_id: self.sqlid}, function(json) {
			$("#"+self.id+" .todolist-users").empty();
			for(var i in json) {
				var liHTML = "<li>"+json[i].email+"</li>";
				$("#"+self.id+" .todolist-users").append(liHTML);
			}
			
			$("#"+self.id).children(".todolist-tasks").slideToggle(1000);
			$("#"+self.id).children(".todolist-sharing").slideToggle(1000);
		});
	});

	$("#"+self.id+" .delete").live('click', function() {
		$.get('php/delete_todolist.php', {todolist_id: self.sqlid}, function() {
			$("#"+self.id).fadeOut(200);
			list.populate();
		});
	});

	$("#"+self.id+" .close").live('click', function() {
		$("#"+self.id).fadeOut({duration: 1000, easing: 'easeOutBack'}).remove();
	});

	$("#"+self.id).live('click', function() {
		$(this).css({'z-index': max_z});
		max_z += 1;
	});

	self.draw = function() {
		$("#workspace").append(self.HTML);
		$("#"+self.id).css({left: self.x, top: self.y});
		$("#"+self.id).draggable({containment: "#workspace", scroll: false});
	}


	function save() {
	}
}	

$(".tasks a").live('click', function(event) {
	var task_id = parseInt($(this).parent().attr('id')); 
	var modal = new TaskDisplay(task_id);
});

$(".task-checkbox").live('change', function(event) {
	var id = $(this).attr("id");
	var checked = $(this).is(":checked");
	$.get("php/update_task_finished.php", {finished: checked, task_id: id});
});
